package org.smartercontext.local.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.DTO.PaymentMethodDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetUserPaymentMethod;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class AddPaymentMethodServlet
 */
@WebServlet("/AddPaymentMethodServlet")
public class AddPaymentMethodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddPaymentMethodServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	
    		HttpSession session = request.getSession();
    		String username = session.getAttribute("username").toString();
    		
    		if(username!=null){
    			
    			String method = request.getParameter("method");
    			String name = request.getParameter("name");
    			String descrip = request.getParameter("descrip");
    			
    			if(name.length()!=0 && descrip.length()!=0 ){
    			
    				System.err.println(name.length()+"-"+descrip.length());
    				
    			PaymentMethodDTO dto = new PaymentMethodDTO();
    			
    			if(method.equals("Credit card")){
    				
    				String creditCardtype = request.getParameter("methodC");
    				String billingAddress = request.getParameter("billingAddress");
    				String cardNumber = request.getParameter("cardNumber");
    				String expMonth = request.getParameter("expirationMonth");
    				String expYear = request.getParameter("expirationYear");
    				String nameOnCard = request.getParameter("nameOnCard");
    				
    				if(billingAddress==null&&cardNumber==null&&expMonth==null&&expYear==null&&nameOnCard==null){
    					response.sendRedirect("manageContext_PaymentM.jsp?Error=1");
    				}
    				
    				int expMonthI=Integer.parseInt(expMonth);
    				int expYearI = Integer.parseInt(expYear);
    				
    				dto = new PaymentMethodDTO(creditCardtype, name, descrip, billingAddress, cardNumber, expMonthI, expYearI, nameOnCard, null);
    				
    				try{
    					
    				}catch(Exception e){
    					response.sendRedirect("manageContext_PaymentM.jsp?Error=1");
    				}
    			}else{
    				dto.setMethodType(method);
    				dto.setName(name);
    				dto.setDescription(descrip);
    			}
    			
    			GetUserPaymentMethod gupm = new GetUserPaymentMethod();
    			try {
    				ContextManagerController.contextManagerController();
					Model usermodel=ContextManagerController.getUsermodel(username);
					Model usermodelU=gupm.addPaymentMethod(usermodel, username, dto);
					ContextManagerController.integrateContext(usermodelU, username);
					response.sendRedirect("manageContext_PaymentM.jsp");
				} catch (Exception e) {
					e.printStackTrace();
					response.sendRedirect("manageContext_PaymentM.jsp?Error=1");
				}
    			
    			}else{
    				response.sendRedirect("manageContext_PaymentM.jsp?Error=1");
    			}
    			
    		}else{
    			response.sendRedirect("login.jsp?Err=3");
    		}
    	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
